package com.jamal2362.apktool.fragment.files;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import androidx.core.content.ContextCompat;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.FilesFragment;
import com.jamal2362.apktool.util.Settings;
import com.jamal2362.apktool.view.RefreshListView;
import com.jamal2362.apktool.view.TreeView;

public class ApplicationsPage {
	private final View view;
	private final PackageManager mPackageManager;
	private final CharSequence title;
	private TreeView<ErrorTree> apps;
	private ErrorsAdapter adapter;
	private final ApplicationItem installedApps;
	private final ApplicationItem systemApps;
	private ImageView refreshView;
	private Context mContext;

	public ApplicationsPage(Context context) {
		LayoutInflater inflater = LayoutInflater.from(context);
		this.mContext = context;
		this.mPackageManager = context.getPackageManager();
		this.view = inflater.inflate(R.layout.errors, null);
		this.title = context.getText(R.string.apps);
		this.apps = view.findViewById(R.id.errors);
		this.refreshView = view.findViewById(R.id.filesImageView1);
		this.installedApps = new ApplicationItem(context.getText(R.string.apps_installed), context.getText(R.string.apps_installed_sum), false);
		this.systemApps = new ApplicationItem(context.getText(R.string.apps_system), context.getText(R.string.apps_system_sum), true);
	}

	public void init(final FilesFragment frag) {
		adapter = new ErrorsAdapter(){
			@Override
			public boolean shouldShowExpandDrawable() {
				return true;
			}

			@Override
			public void reset() {
				childs.clear();
				refreshApps();
				refresh();
			}
		};
		adapter.reset();
		adapter.addChild(installedApps);
		adapter.addChild(systemApps);
		apps.setAdapter(adapter);
		final ColorFilter[] filter = new PorterDuffColorFilter[2];
		int drawableColor = (Settings.lightTheme ? Color.DKGRAY : Color.WHITE);
		int accentColor = ContextCompat.getColor(mContext, (R.color.accent_material));
		filter[0] = new PorterDuffColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
		filter[1] = new PorterDuffColorFilter(drawableColor, PorterDuff.Mode.SRC_ATOP);
		refreshView.getBackground().setColorFilter(filter[1]);
		refreshView.getBackground().setAlpha(0);
		final float scale = apps.getDistance() / 255;
		apps.setRefreshListener(new RefreshListView.RefreshListener()
			{
				@Override
				public void onRefresh() {
					init(frag);
					refreshView.getBackground().setColorFilter(filter[1]);
					refreshView.getBackground().setAlpha(0);
					refreshView.setVisibility(View.GONE);
				}

				@Override
				public void update(final float distance, float refreshDistance) {
					int opacity = Math.round(distance / scale);
					if (opacity > 255) {
						opacity = 255;
					}
					if (opacity <= 255 && opacity >= 10) {
						Drawable refreshDrawable = apps.rotateDrawable(refreshView.getBackground(), opacity);
						refreshDrawable.setColorFilter(opacity == 255 ? filter[0] : filter[1]);
						refreshDrawable.setAlpha(opacity);
						refreshView.setBackground(refreshDrawable);
						refreshView.setVisibility(View.VISIBLE);
					} else {
						refreshView.getBackground().setColorFilter(filter[1]);
						refreshView.getBackground().setAlpha(0);
						refreshView.setVisibility(View.GONE);
					}
				}
			});
	}

	public View getView() {
		return view;
	}

	public CharSequence getTitle() {
		return title;
	}

	protected void refreshApps() {
		ApplicationItem installedApps = this.installedApps;
		ApplicationItem systemApps = this.systemApps;
		installedApps.reset();
		systemApps.reset();
		PackageManager mPackageManager = this.mPackageManager;
		for (PackageInfo pkg: mPackageManager.getInstalledPackages(0)) {
			if (pkg.applicationInfo.sourceDir.startsWith("/data/"))
				installedApps.addApp(pkg, mPackageManager);
			else
				systemApps.addApp(pkg, mPackageManager);
		}
		installedApps.sort();
		systemApps.sort();
	}
}
